# coding:utf-8

from wtforms import StringField, TextAreaField, SubmitField, IntegerField
from wtforms_components import SelectField
from wtforms.validators import DataRequired, Optional


from app.forms import (
    WphForm, NotChoices, NonValidatingSelectField, MyDateTimeField
)
from app.op.models import WxUser, User
from .constant import WxMenu


class WechatTemplateForm(WphForm):
    """
    新建/编辑短信模板表单
    """

    name = StringField('模板名称', validators=[DataRequired()])
    code = StringField(
        '模板代码',
        validators=[DataRequired()],
        render_kw={'placeholder': '此编码为本系统独立维护的代码'}
    )
    template_code = StringField(
        '模板编码',
        validators=[DataRequired()],
        render_kw={'placeholder': '此编码为微信公众号配置的模板编码'}
    )
    content = TextAreaField(
        '模板内容', validators=[DataRequired()],
        render_kw={'rows': 15}
    )
    params = StringField('模板包含参数', validators=[Optional()], description='空格隔开')

    submit = SubmitField('保存')


class SearchWechatLogForm(WphForm):
    """
    搜索微信发送记录表单
    """

    user_mobile = StringField('电话号码', validators=[Optional()])
    user_nickname = StringField('用户昵称', validators=[Optional()])

    submit = SubmitField('查询')


class SearchWechatListForm(WphForm):
    """
    搜索微信用户列表表单
    """

    subscribe = NonValidatingSelectField(
        '是否关注',
        validators=[Optional()],
        choices=[(-1, '全部'), (0, '未关注'), (1, '已关注')],
        coerce=int
    )
    invite_user_id = NonValidatingSelectField(
        '邀请人', validators=[Optional()], coerce=int
    )
    is_invite_user = NonValidatingSelectField(
        '邀请人筛选',
        validators=[Optional()],
        choices=NotChoices + [(1, '无邀请人'), (2, '有邀请人')],
        coerce=int,
        default=NotChoices
    )
    nickname = StringField('微信昵称', validators=[Optional()])
    subscribe_time_begin = MyDateTimeField('开始关注时间', validators=[Optional()])
    subscribe_time_end = MyDateTimeField('结束关注时间',  validators=[Optional()])

    submit = SubmitField('查询')

    def __init__(self, only_ground=False, *args, **kwargs):
        super(SearchWechatListForm, self).__init__(*args, **kwargs)

        user_ids = WxUser.get_all_invite_user_ids(only_ground)
        users = User.get_list_by_ids(user_ids)

        self.invite_user_id.choices = NotChoices + \
            [
                (user.id, user.nickname)
                for user in users
            ]
        self.invite_user_id.default = NotChoices[0][0]


class WechatMenuForm(WphForm):
    """
    created by chenhj at 2017/8/3
    微信菜单表单
    """
    level = SelectField('级别', validators=[DataRequired()], choices=[
        (1, '一级菜单'), (2, '二级菜单')
    ], coerce=int)
    pid = SelectField('父级菜单', validators=[Optional()], coerce=int)
    idx = IntegerField('排序', validators=[DataRequired()])
    name = StringField('名称', validators=[DataRequired()])
    type = SelectField('类型', validators=[DataRequired()], choices=WxMenu.to_options())
    url = StringField('网页链接', validators=[Optional()])
    key = StringField('key', validators=[Optional()])
    appid = StringField('小程序appid', validators=[Optional()])
    pagepath = StringField('小程序pagepath', validators=[Optional()])

    submit = SubmitField('提交')
